<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" @click="addVisible=true" size="mini">添加分类</el-button>
        <!-- 添加分类的对话框 -->
      <el-dialog title="添加文章分类" @closed="reset" :visible.sync="addVisible" width="35%">
        <span>
          <!-- 添加的表单 -->
          <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="70px">
            <el-form-item label="分类名称" prop="cate_name">
              <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
            </el-form-item>
            <el-form-item label="分类别名" prop="cate_alias">
              <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
            </el-form-item>
          </el-form>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="addVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="addArt">添 加</el-button>
        </span>
      </el-dialog>
      <!-- 修改文章分类的对话框 -->
        <el-dialog @closed="EditReset" title="修改文章分类" :visible.sync="editVisible" width="35%">
          <span>
            <!-- 修改的表单 -->
            <el-form :model="editForm" :rules="addRules" ref="editRef" label-width="100px">
              <el-form-item label="分类名称" prop="cate_name">
                <el-input v-model="editForm.cate_name"></el-input>
              </el-form-item>
              <el-form-item label="分类别名" prop="cate_alias">
                <el-input v-model="editForm.cate_alias"></el-input>
              </el-form-item>
            </el-form>
          </span>
          <span slot="footer" class="dialog-footer">
            <el-button size="mini" @click="editVisible = false">取 消</el-button>
            <el-button size="mini" type="primary" @click="ChangeInp">确 定</el-button>
          </span>
        </el-dialog>
      </div>
     <!-- 表格区域 -->
      <el-table stype="width: 100%;" :data="catelist" border stripe>
        <el-table-column label="序号" prop="id" type="index" width="100"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
         <template v-slot="scope">
            <el-button type="primary" @click="showEdit(scope.row.id)"  size="mini">修改</el-button>
          <el-button type="danger" @click="delEdit(scope.row.id)" size="mini">删除</el-button>
         </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      catelist: [],
      addVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      // 修改
      editVisible: false,
      editForm: {}
    }
  },
  methods: {
    // 删除文章
    async   delEdit (id) {
      try {
        if (id === 1 || id === 2) {
          return this.$message.error('不可以删除')
        }
        await this.$confirm('确认要删除分类吗?', '删除分类提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        const res = await this.$http.delete('/my/cate/del', { params: { id } })
        if (res.data.code !== 0) return this.$message.error('删除失败')
        this.$message.success('删除成功')
        // 关闭对话框
        this.editVisible = false
        // 重新渲染
        this.initCate()
      } catch (e) {
        if (e === 'cancel') {
          this.$message.error('删除失败')
        }
      }
    },
    // 修改文章
    ChangeInp () {
      // 自定义验证
      this.$refs.editRef.validate(async v => {
        // 判断
        if (!v) return
        const res = await this.$http.put('/my/cate/info', this.editForm)
        if (res.data.code !== 0) return this.$message.error('修改失败')
        // 成功
        this.$message.success('修改成功')
        // 关闭对话框
        this.editVisible = false
        // 重新渲染表单
        this.initCate()
      })
    },
    // 重置表单
    EditReset () {
      this.$refs.editRef.resetFields()
    },
    // 修改文章
    async  showEdit (id) {
      // 发送请求
      const res = await this.$http.get('/my/cate/info', { params: { id } })
      // 传入id
      if (res.data.code === 0) {
        // 获取到数据
        this.editForm = res.data.data
        // 展示对话框
        this.editVisible = true
      }
    },
    // 获取数据
    addArt () {
      // 表单自定义
      this.$refs.addRef.validate(async v => {
        if (!v) return
        const ret = await this.$http.post('/my/cate/add', this.addForm)
        if (ret.data.code !== 0) return this.$message.error('添加文章失败')
        // 成功
        this.$message.success('添加文章成功')
        // 关闭 对话框
        this.addVisible = false
        // 重新渲染页面
        this.initCate()
      })
    },
    // 初始化数据
    async initCate () {
      const res = await this.$http.get('/my/cate/list')
      //  判断
      // console.log(res.data)
      if (res.data.code !== 0) return
      // 获取到
      this.catelist = res.data.data
    },
    reset () {
      this.$refs.addRef.resetFields()
    }
  },
  // 初始化数据
  created () {
    this.initCate()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
